﻿Option Strict On

Imports Sunts.HyperV.Common


Public Class frmHostSetup
    Private HyperVHostCollection As HyperVHostCollection

    Private Sub HyperVHostSetup_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        HyperVHostCollection = HyperVHost.SelectAll(My.Settings.ConnectionString)
        FillExistingServers()
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        HyperVHostCollection = Nothing
        Me.Close()
    End Sub


    Private Sub btnAddLocalhost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLocalhost.Click
        Dim HyperVHost As HyperVHost
        Dim ConnectableState As HyperVHost.ConnectableState


        Try
            If Me.HyperVHostCollection.ContainsHyperVHost("localhost") Then
                MessageBox.Show("The Hyper-V Host Server will not be added to the database as it already exists")
                HyperVHost = HyperVHostCollection.GetHyperVHost("localhost")
            Else
                HyperVHost = New HyperVHost
                HyperVHost.Hostname = "localhost"
                HyperVHost.Username = Me.txtHostUsername.Text
                HyperVHost.Password = Me.txtHostPassword.Text
                HyperVHost.ComputerName = Environment.MachineName

                ConnectableState = HyperVHost.GetConnectionState

                Select Case ConnectableState
                    Case HyperVHost.ConnectableState.Connectable
                        HyperVHost.HostID = HyperVHost.Insert(My.Settings.ConnectionString)

                        If HyperVHost.HostID <> 0 Then
                            Using frmDetectedServers As New frmDetectedServers
                                frmDetectedServers.HyperVHostMachine = HyperVHost
                                frmDetectedServers.ShowDialog()
                            End Using
                        End If

                        FillExistingServers()
                    Case HyperVHost.ConnectableState.NamespaceNotFound
                        MessageBox.Show("The virtualisation WMI namespace could not be found, is the remote system running Hyper-V?" & ControlChars.CrLf & ControlChars.CrLf & _
                                        "The exception that was caught was: " & ControlChars.CrLf & _
                                        HyperVHost.Exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Case HyperVHost.ConnectableState.Unknown
                        MessageBox.Show("An unknown exception occurred," & ControlChars.CrLf & ControlChars.CrLf & _
                                        "The exception that was caught was: " & ControlChars.CrLf & _
                                        HyperVHost.Exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Select

            End If

        Catch ex As Exception
            MessageBox.Show("frmHostSetup: btnAddLocalhost_Click: " & ex.Message)
        Finally
            HyperVHost = Nothing
            ConnectableState = Nothing
        End Try
    End Sub

    Private Sub btnAddRemote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRemote.Click
        Dim HyperVHost As HyperVHost
        Dim ConnectableState As HyperVHost.ConnectableState

        Try
            If Me.txtHostName.Text = String.Empty Or Me.txtHostUsername.Text = String.Empty Or Me.txtHostPassword.Text = String.Empty Then
                MessageBox.Show("Could not add the server, make sure you have filled in the hostname and credentials correctly")
                Exit Sub
            Else
                HyperVHost = New HyperVHost
                HyperVHost.Hostname = Me.txtHostName.Text
                HyperVHost.Username = Me.txtHostUsername.Text
                HyperVHost.Password = Me.txtHostPassword.Text

                ConnectableState = HyperVHost.GetConnectionState

                Select Case ConnectableState
                    Case HyperVHost.ConnectableState.Connectable
                        HyperVHost.HostID = HyperVHost.Insert(My.Settings.ConnectionString)

                        If HyperVHost.HostID <> 0 Then
                            Using frmDetectedServers As New frmDetectedServers
                                frmDetectedServers.HyperVHostMachine = HyperVHost
                                frmDetectedServers.ShowDialog()
                            End Using
                        End If

                        FillExistingServers()
                    Case HyperVHost.ConnectableState.AccessIsDenied
                        MessageBox.Show("Could not connect to the remote server." & ControlChars.CrLf & _
                                        "Possible reason for this include: " & ControlChars.CrLf & _
                                        " - Invalid Credentials" & ControlChars.CrLf & _
                                        " - WMI Errors" & ControlChars.CrLf & _
                                        " - Firewall Restrictions" & ControlChars.CrLf & ControlChars.CrLf & _
                                        "The exception that was caught was: " & ControlChars.CrLf & _
                                         HyperVHost.Exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Case HyperVHost.ConnectableState.NamespaceNotFound
                        MessageBox.Show("The virtualisation WMI namespace could not be found, is the remote system running Hyper-V?" & ControlChars.CrLf & ControlChars.CrLf & _
                                        "The exception that was caught was: " & ControlChars.CrLf & _
                                        HyperVHost.Exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Case HyperVHost.ConnectableState.Unknown
                        MessageBox.Show("An unknown exception occurred," & ControlChars.CrLf & ControlChars.CrLf & _
                                        "The exception that was caught was: " & ControlChars.CrLf & _
                                        HyperVHost.Exception.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End Select

            End If
        Catch ex As Exception
            MessageBox.Show("frmHostSetup: btnAddRemote_Click: " & ex.Message)
        Finally
            HyperVHost = Nothing
            ConnectableState = Nothing
        End Try
    End Sub

    Private Sub FillExistingServers()
        Try
            ListView1.Items.Clear()
            For Each HyperVHost As HyperVHost In HyperVHostCollection
                Me.ListView1.Items.Add(HyperVHost.Hostname, 4)
            Next
        Catch ex As Exception
            MessageBox.Show("frmHostSetup: FillExistingServers: " & ex.Message)
        End Try
    End Sub



End Class